/*
 * GoogleTranslateConnection.java
 * 
 *  Copyright (C) - 2008 - Juanbc - Aleval
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.bts.translateTool.back;

import com.bts.util.DMesg;
import com.google.api.translate.*;


/**
 * Clase que gestiona la coneccion entre la aplicacion y la libreria de traduccion de google
 * @author juan
 */
public class GoogleTranslateConnection{
    /**
     * lista de lenguajes a utilizar
     */
    private java.util.ArrayList<NameAndGC> langs;
    
    /**
     * crea una nuevo objeto
     */
    public GoogleTranslateConnection(){
        langs = new java.util.ArrayList<NameAndGC>();
        langs.add(new NameAndGC("Español",Language.SPANISH));
        langs.add(new NameAndGC("Inglés", Language.ENGLISH));
        langs.add(new NameAndGC("Arabe", Language.ARABIC));
        langs.add(new NameAndGC("Chino",Language.CHINESE));
        langs.add(new NameAndGC("Chino (Simplificado)",Language.CHINESE_SIMPLIFIED));
        langs.add(new NameAndGC("Chino (Tradicional)",Language.CHINESE_TRADITIONAL));
        langs.add(new NameAndGC("Francés",Language.FRENCH));
        langs.add(new NameAndGC("Alemán",Language.GERMAN));
        langs.add(new NameAndGC("Italiano",Language.ITALIAN));
        langs.add(new NameAndGC("Japonés",Language.JAPANESE));
        langs.add(new NameAndGC("Koreano",Language.KOREAN));
        langs.add(new NameAndGC("Portugués",Language.PORTUGESE));
        langs.add(new NameAndGC("Ruso",Language.RUSSIAN));
        DMesg.outln("Filled Lannguages");
    }
    
    /**
     * retorna los nombres de los lenguajes en un arreglo
     * @return String[] arreglo de los nombres de lenguajes
     */
    public String[] getLanguagesNames(){
        String[] aux=new String[langs.size()];
        for(int i=0;i<langs.size();i++){
            aux[i]=langs.get(i).getName();
        }
        return aux;
    }
    
    /**
     * traduce un texto determinado
     * @param s texto a traducir
     * @param fromLanguage
     * @param toLanguage
     * @return String texto traducido
     * @throws com.bts.translateTool.back.GoogleTranslateApiException
     */
    public String getTranslation(String s, String fromLanguage, String toLanguage) throws GoogleTranslateApiException{
        fromLanguage=this.getGC(fromLanguage);
        toLanguage=this.getGC(toLanguage);
        try {
            s=Translate.translate(s,fromLanguage,toLanguage);
        } catch (Exception ex) {
            throw new GoogleTranslateApiException();
        }
        DMesg.outln("Google Return: " + s);
        return s.replaceAll("<br>","\n").replaceAll("&lt;br&gt;", "\n");
    }
    
    /**
     * retorna el codigo de google para algun nombre de lenguaje
     * @param languageName
     * @return String nombre del lenguaje
     * @throws com.bts.translateTool.back.GoogleTranslateApiException
     */
    private String getGC(String languageName) throws GoogleTranslateApiException{
        for(int i=0;i<langs.size();i++){
            if(langs.get(i).getName().compareTo(languageName)==0){
                return langs.get(i).getGoogleCode();
            }
        }
        throw new GoogleTranslateApiException("Not exist a google code for thar Language name");
    }
    
    /**
     * Clase Interna que relaciona un Nombre con el codigo de lenguage de Google  
     */ 
    private class NameAndGC{
        /**
         * nombre del lenguaje
         */
        private String name;
        /**
         * codigo google del lenguaje
         */
        private String googleCode;
        
        /**
         * crea un nuevo objeto relacionando nombre con un codigo
         * @param name
         * @param code
         */
        public NameAndGC(String name, String code){
            this.name=name;
            this.googleCode=code;
        }

        public String getName() {
            return name;
        }

        public String getGoogleCode() {
            return googleCode;
        }
    }
    
}
